package inspire13.duckhuntroid;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

public class Sprite 
{
	private Vector<Animation> mAnimations;
	private Animation mActiveAnimation;
	private int mX;
	private int mY;
	private int mWidth;
	private int mHeight;
	private float mAnimationDelay;
	private float mAnimationTimer;
	private float mScaleX;
	private float mScaleY;
	
	public Sprite(int width, int height)
	{
		mAnimations = new Vector<Animation>();
		mActiveAnimation = null;
		mX = 0;
		mY = 0;
		mWidth = width;
		mHeight = height;
		mAnimationDelay = 0;
		mAnimationTimer = 0.0f;
		mScaleX = 1.0f;
		mScaleY = 1.0f;
	}
	
	public void AddAnimation(Animation animation)
	{
		mAnimations.add(animation);
	}
	
	public void PlayAnimation(String animationName, float animationDelay)
	{
		mActiveAnimation = null;
		
		for(int i = 0; i < mAnimations.size(); i++)
		{
			if(mAnimations.elementAt(i).GetName() == animationName)
			{
				mActiveAnimation = mAnimations.elementAt(i);
				break;
			}
		}
			
		mAnimationDelay = animationDelay;	
	}
	
	public void UpdateAnimation(float deltaTime)
	{
		if(mActiveAnimation == null)
			return;
		
		if(mAnimationTimer > mAnimationDelay)
		{
			mActiveAnimation.Play();	
			mAnimationTimer = 0.0f;
		}
		
		mAnimationTimer += deltaTime;
	}
	
	public void Draw(Canvas canvas)
	{
		if(mActiveAnimation != null)
		{
			Drawable image = mActiveAnimation.CurrentFrame();
			
			if(image != null)
			{
				int right  = mX + mWidth;
				int bottom = mY + mHeight;
				image.setBounds(mX, mY, right, bottom);
				image.draw(canvas);
			}
		}
	}
	
	public void SetLocation(int x, int y)
	{
		mX = x;
		mY = y;
	}
	
	public int GetWidth()
	{
		return mWidth;
	}
	
	public int GetHeight()
	{
		return mHeight;
	}
	
	public void SetScale(float scaleX, float scaleY)
	{
		mScaleX = scaleX;
		mScaleY = scaleY;
		mWidth  = (int)(mWidth  * mScaleX);
		mHeight = (int)(mHeight * mScaleY);
	}
}
